package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.POIUtils;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;


    //下载excel文件模板
    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        //设置响应类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //设置响应头
        response.setHeader("Content-Disposition","ordersetting_template.xlsx");
        //获取workbook对象
        Workbook workbook = new XSSFWorkbook();
        //获取sheet对象
        Sheet sheet = workbook.createSheet("预约设置");
        //创建表头
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("预约人数");
        //通过输出流下载到客户端浏览器
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream); //将数据写入到输出流中
            log.info("文件下载成功");

            //关闭资源
            outputStream.close();
            workbook.close();
        }
        catch (Exception e){
            log.info("文件下载失败");
        }
    }

    /**
     * 上传excel文件
     * @return
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> data = new ArrayList<>();
            for (String[] strings : list) {
                String orderdate = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderdate),new Integer(number));
                data.add(orderSetting);
                //调用service
                orderSettingService.addNumberByOrderDate(data);
                return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return null;
    }



    /**
     * 根据日期修改可预约人数
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        log.info("修改预约人数：{}",orderSetting);
        try {
            orderSettingService.editNumberByOrderDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据年月查询预约设置数据
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month) throws Exception {
        log.info("根据年月查询预约设置数据：{}",month);
//        try {
            //调用service
            List<Map> list = orderSettingService.findOrderSettingByMonth(month);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
//        } catch (Exception e) {
//            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
//        }
    }




}
